package com.whc.search.controller;

import com.whc.common.exception.BizCodeEnume;
import com.whc.common.to.es.SkuEsModel;
import com.whc.common.utils.R;
import com.whc.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * ES保存 Controller
 *
 * @author angaoen
 */
@Slf4j
@RequestMapping("/search/save")
@RestController
public class ElasticSaveController {

    /**
     * 保存商品 Service
     */
    @Resource
    private ProductSaveService productSaveService;

    /**
     * 上架商品
     *
     * @param skuEsModels 上架商品列表
     * @return 统一返回结果
     */
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> skuEsModels) {
        Boolean isHasError;
        try {
            isHasError = productSaveService.productSaveService(skuEsModels);
        } catch (IOException e) {
            log.error("ElasticSaveController商品上架错误：{}", e.getMessage());
            return R.error(BizCodeEnume.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnume.PRODUCT_UP_EXCEPTION.getMsg());
        }
        return isHasError ? R.error() : R.ok();
    }
}
